/*=============jQuery plugin/ Object function===============*/
jQuery.fn.isExist = function () {
    return jQuery(this).length > 0;
};

jQuery.fn.isNullOrEmpty = function () {
    if (jQuery(this).val() === null || $.trim(jQuery(this).val()) === "") {
        return true;
    } else {
        return false;
    }
};

String.prototype.isEmail = function () {
    var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(this);
};

jQuery.fn.stringIsValidate = function (minleng, maxlenth) {
    if (jQuery.trim(jQuery(this).val()).length < minleng || jQuery.trim(jQuery(this).val()).length > maxlenth) {
        jQuery(this).focus();
        return false;
    } else {
        return true;
    }
};


String.prototype.replaceAll = function (
        strTarget, // The substring you want to replace
        strSubString // The string you want to replace in.
        ) {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);
    while (intIndexOfMatch !== -1) {
        strText = strText.replace(strTarget, strSubString)
        intIndexOfMatch = strText.indexOf(strTarget);
    }
    return(strText);
};

function CurrencyFormatted(n) {
    return (Math.round(n * 100) / 100).toLocaleString();
}

String.prototype.subString = function (len) {
    if (this.length >= len) {
        return this.substring(0, len) + "...";
    }
    return this;
};

String.prototype.addslashes = function () {
    var str = this;
    str = str.replace(/\\/g, '\\\\');
    str = str.replace(/\'/g, '\\\'');
    str = str.replace(/\"/g, '\\"');
    str = str.replace(/\0/g, '\\0');
    return str;
};

String.prototype.stripslashes = function () {
    var str = this;
    str = str.replace(/\\'/g, '\'');
    str = str.replace(/\\"/g, '"');
    str = str.replace(/\\0/g, '\0');
    str = str.replace(/\\\\/g, '\\');
    return str;
};


var UI = {
    optSpiner: {
        lines: 12, // The number of lines to draw 12
        length: 5, // The length of each line 5
        width: 2, // The line thickness 2
        radius: 7, // The radius of the inner circle 7 
        corners: 1, // Corner roundness (0..1) 1
        rotate: 0, // The rotation offset 0
        direction: 1, // 1: clockwise, -1: counterclockwise
        color: '#fff', // #rgb or #rrggbb or array of colors
        speed: 1, // Rounds per second
        trail: 60, // Afterglow percentage
        shadow: false, // Whether to render a shadow
        hwaccel: false, // Whether to use hardware acceleration
        className: 'spinner', // The CSS class to assign to the spinner
        zIndex: 9999 // The z-index (defaults to 2000000000)
    },
    setHover: function () {
        var $this = $(this);
        $this.bind("touchstart", function () {
            var $this = $(this);
            $this.addClass("hover")
                    .bind("touchend", function () {
                        $this.removeClass("hover");
                    });
        });
    },
    loadJs: function (url, success, error) {
        $.ajax({
            url: url,
            dataType: "script",
            async: false,
            timeout: 5000,
            success: function () {
                if (typeof success == 'function') {
                    success();
                }
            },
            error: function () {
                if (typeof error == 'function') {
                    success();
                }
            }
        });
    },
    mobileDetect: function () {
        var checker = {
            iphone: navigator.userAgent.match(/(iPhone|iPod|iPad)/),
            blackberry: navigator.userAgent.match(/BlackBerry/),
            android: navigator.userAgent.match(/Android/),
            windowphone: navigator.userAgent.match(/Windows Phone/),
            moto: navigator.userAgent.match(/MOTOROLA/),
            Windows: navigator.userAgent.match(/IEMobile/),
            Nokia: navigator.userAgent.match(/Nokia/),
            opera: navigator.userAgent.match(/Opera Mini/)
        };
        if (checker.android) {
            return "android";
        } else if (checker.iphone) {
            return "ios";
        } else {
            return "other";
        }
    }
}

/*
 *@Toast Class
 */
var Toast = new function () {
    this.flag = function () {
        if ($(".Toast_message").length == 0 && $(".Toast_wait").length == 0) {
            return true;
        }
        return false;
    };
    this.show = function (options) {
        if (this.flag()) {
            var params = {
                id: "UI_Toast_id",
                type: "text",
                text: "",
                position: "center",
                height: "auto",
                width: "auto",
                size: "medium",
                autoClose: 2000
            };
            $.extend(params, options);
            var type = "";
            switch (params.type) {
                case "success":
                    type = '<span class="fa fa-check"></span>';
                    break;
                case "failure":
                    type = '<span class="fa fa-frown-o"></span>';
                    break;
                case "warning":
                    type = '<span class="fa fa-exclamation-circle"></span>';
                    break;
                default:
                    break;
            }

            var position = "";
            switch (params.position) {
                case "top":
                    position = 'Toast_message_top ';
                    break;
                case "bottom":
                    position = 'Toast_message_bottom ';
                    break;
                default:
                    position = 'Toast_message_center ';
                    break;
            }
            var size = params.size == "large" ? "Toast_message_large " : "";
            $("body").append('<div id="' + params.id + '" style="' + params.height + params.width + '" class="Toast_message ' + size + position + '"><div class="Toast_message_inner">' + type + params.text + '</div></div>');
            if (params.autoClose > 0) {
                setTimeout(function () {
                    $("#" + params.id).remove();
                }, params.autoClose);
            }
        }
    };
    this.close = function (id) {
        $(".Toast_message,.Toast_wait_wrapper").remove();
    };
    this.showWait = function (options) {
        if (this.flag()) {
            var params = {
                text: "",
                width: "",
                height: "",
                autoClose: 0,
                skin: "dark", //dark | white
                mode: "horizontal", //vertical | horizontal,
                position: "center", //top | bottom | center
            };
            $.extend(params, options);
            var type = params.text == "" ? "icon" : "icon_text";
            var html = '<div class="Toast_wait_wrapper">\n\
                            <div style="width:' + params.width + ";height:" + params.height + '" class="Toast_wait ' + type + ' ' + params.skin + ' ' + params.mode + ' ' + params.position + '">\n\
                                <div class="Spinner_icon"></div><div class="Spinner_text">' + params.text + '</div>\n\
                            </div>\n\
                        </div>';
            $("body").append(html);
            if (params.mode == "vertical") {
                UI.optSpiner.lines = 12;
                UI.optSpiner.length = 4;
                UI.optSpiner.width = 1;
                UI.optSpiner.radius = 5;
            }
            if (params.skin == "white") {
                UI.optSpiner.color = "#000";
            }
            new Spinner(UI.optSpiner).spin(document.querySelector('.Toast_wait .Spinner_icon'));
            if (params.autoClose) {
                setTimeout(function () {
                    $(".Toast_wait_wrapper").remove();
                }, params.autoClose);
            }
        }
    };
    this.showMessage = function (options) {
        var params = {
            text: "",
            width: "",
            height: "",
            autoClose: 0,
            skin: "dark", //dark | white
            mode: "horizontal", //vertical | horizontal,
            position: "center", //top | bottom | center
            typeMessage: "success",
        };
        $.extend(params, options);
        var type = params.text == "" ? "icon" : "icon_text";
        var icon = "";
        switch (params.typeMessage) {
            case "success":
                icon = '<span class="fa fa-check"></span>';
                break;
            case "failure":
                icon = '<span class="fa fa-frown-o"></span>';
                break;
            case "warning":
                icon = '<span class="fa fa-exclamation-circle"></span>';
                break;
            default:
                icon = '<span class="fa fa-check"></span>';
                break;
        }
        if (this.flag()) {
            var html = '<div class="Toast_wait_wrapper">\n\
                            <div style="width:' + params.width + ";height:" + params.height + '" class="Toast_wait ' + type + ' ' + params.skin + ' ' + params.mode + ' ' + params.position + '">\n\
                                <div class="Spinner_icon">' + icon + '</div><div class="Spinner_text">' + params.text + '</div>\n\
                            </div>\n\
                        </div>';
            $("body").append(html);
        } else {

            $(".Toast_wait").html('<div class="Spinner_icon">' + icon + '</div><div class="Spinner_text">' + params.text + '</div>')
                    .attr("class", "Toast_wait " + type + ' ' + params.skin + ' ' + params.mode + ' ' + params.position)
                    .attr("style", 'width:' + params.width + ";height:" + params.height);
        }

        if (params.autoClose) {
            setTimeout(function () {
                $(".Toast_wait_wrapper").remove();
            }, params.autoClose);
        }
    };
};

var Dialog = new function () {
    this.IsShow = false;
    this.effectIn = "";
    this.effectOut = "";
    this.showConfirm = function (options) {
        if (!this.IsShow) {
            this.IsShow = true;
            var params = {
                title: "Thông báo",
                content: "Dialog content",
                buttons: [{
                        id: "Confirm_cancel",
                        text: "Đóng lại",
                        type: "bold"
                    }],
                effectIn: "ZoomIn_3",
                effectOut: "ZoomOut_4",
                height: "178px",
                event: ""
            };
            $.extend(params, options);
            this.effectIn = params.effectIn;
            this.effectOut = params.effectOut;
            var buttons = '';
            var length = 0;
            $.each(params.buttons, function (i, item) {
                length++;
                var type = (item.type !== undefined ? item.type : '');
                buttons += "<button class='" + type + "' id='" + item.id + "'>" + item.text + "</button>";
            });
            var html = '<div class="UI_Dialog UI_Dialog_' + UI.mobileDetect() + ' modal"><div class="UI_Dialog_layout"></div>\n\
    <div class="UI_Dialog_content" style="height:' + params.height + '"><div class="modal-dialog ' + params.effectIn + '">\n\
        <div class="modal-content">\n\
            <div class="modal-header">\n\
                <h4 class="modal-title">' + params.title + '</h4>\n\
            </div>\n\
            <div class="modal-body">' + params.content + '</div>\n\
            <div class="modal-footer Button_' + length + '">' + buttons + '</div>\n\
        </div></div><!-- /.modal-content -->\n\
    </div><!-- /.modal-dialog -->\n\
</div><!-- /.modal -->';
            $("body").append(html);
            if (typeof params.event == 'function')
                params.event();
            setTimeout(function () {
                $(".UI_Dialog").removeClass(params.effectIn);
            }, 300);

            $(document).on("tap", "#Confirm_cancel", function () {
                Dialog.close();
            });
        }
    };
    this.close = function (success) {
        this.IsShow = false;
        $(".modal-dialog").addClass(this.effectOut);
        $(".UI_Dialog_layout").css("opacity", "0");
        setTimeout(function () {
            $(".UI_Dialog").remove();
            if (typeof success == 'function') {
                success();
            }
        }, 300);
    }
};
